#include "declarations.h"
using namespace std;
void train::List()
{
	ifstream input;
	input.open("trains.txt");
	char a[100];
	int i=0;
		while (input.good())
		{
			input.getline(a,99);
			cout<<i<<": "<<a<<'\n';
			i++;
		}
	input.close();
	return;
}
void train::Add()
{
	char a[200];
	cout<<"insert train name ";
	cin.getline(a,20);
	strcat(a,", ");
	char oper[150];
	cout<<"train seat count ";
	cin.getline(oper,10);
	strcat(a,oper);
	strcat(a,", ");
	cout<<"train comment ";
	cin.getline(oper,150);
	strcat(a,oper);
	ofstream output;
	output.open("trains.txt",ofstream::app);
	output<<a;
	output.close();

}
char* train::GetTrainID(char* input,char* output)
{
	int i=0;
	while (input[i]!=',')
	{
		output[i]=input[i];
		i++;
	}
	output[i]='\0';
	return output;

}
void train::Delete(int line)
{
	ifstream instream;
	instream.open("trains.txt");
	int i=0;
	char a[200];
	train train;
	schedule schedule;
	char random[50];
	while (instream.good())
	{
		instream.getline(a,199);
		if (i==line)
		{
			if (schedule.CheckSchedule(train.GetTrainID(a,random))!=-1)
			{
				cout<<train.GetTrainID(a,random)<<'\n';
				cout<<"error train used in schedule";
				return;
			}
		}
		else i++;
	}
	instream.close();
	ofstream outstream;
	outstream.open("trains1.txt",ofstream::app);
	ifstream instream1;
	instream1.open("trains.txt");
	i=0;
	while (instream1.good())
	{
		instream1.getline(a,199);
		if (i!=line)
		{
			cout<<a<<endl;
			outstream<<a<<'\n';
			i++;
		}
		else i++;
	}
	instream1.close();
	outstream.close();
	
	remove("trains.txt");
	rename("trains1.txt","trains.txt");
}
void train::Edit(int line)
{
	ifstream instream;
	instream.open("trains.txt");
	int i=0;
	char a[200];
	train train;
	schedule schedule;
	char random[50];
	while (instream.good())
	{
		instream.getline(a,199);
		if (i==line)
		{
			if (schedule.CheckSchedule(train.GetTrainID(a,random))!=-1)
			{
				cout<<train.GetTrainID(a,random)<<'\n';
				cout<<"error train used in schedule";
				return;
			}
		}
		else i++;
	}
	instream.close();
	ofstream outstream;
	outstream.open("trains1.txt",ofstream::app);
	ifstream instream1;
	instream1.open("trains.txt");
	i=0;
	while (instream1.good())
	{
		instream1.getline(a,199);
		if (i!=line)
		{
			cout<<a<<endl;
			outstream<<a<<'\n';
			i++;
		}
		else i++;
	}
	instream1.close();
	outstream.close();
	remove("trains.txt");
	rename("trains1.txt","trains.txt");
	cin.get();
	train.Add();
}


	



train::train(void)
{
}


train::~train(void)
{
}
